package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.OrderDetail;
import com.itheima.domain.Orders;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     *
     * @param orders
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        ordersService.submit(orders);

        return R.success("下单成功!");
    }

    /**
     * 订单明细
     *
     * @param page
     * @param pageSize
     * @param number
     * @param orderTime
     * @param checkoutTime
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, String number, String orderTime, String checkoutTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLqw = new LambdaQueryWrapper<>();
        ordersLqw.eq(number != null, Orders::getNumber, number);
        ordersLqw.ge(orderTime != null, Orders::getOrderTime, orderTime)
                .le(checkoutTime != null, Orders::getCheckoutTime, checkoutTime);
        ordersService.page(pageInfo, ordersLqw);
        return R.success(pageInfo);
    }

    /**
     * 修改派送状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R update(@RequestBody Orders orders) {
        LambdaQueryWrapper<Orders> ordersLqw = new LambdaQueryWrapper<>();
        ordersLqw.eq(orders.getId() != null, Orders::getId, orders.getId());
        Orders ordersOne = ordersService.getOne(ordersLqw);
        ordersOne.setStatus(orders.getStatus());
        ordersService.updateById(ordersOne);
        return R.success(ordersOne);
    }

    @GetMapping("/userPage")
    public R userpage(int page, int pageSize) {

        long userid = BaseContext.getThreadLocal();
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> orderslqw = new LambdaQueryWrapper<>();
        orderslqw.eq(Orders::getUserId, userid);
        orderslqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, orderslqw);

        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage, ordersDtoPage,"records");
        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();

        List<Orders> records = ordersPage.getRecords();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            LambdaQueryWrapper<OrderDetail> orderDetailLqw = new LambdaQueryWrapper<>();
            orderDetailLqw.eq(OrderDetail::getOrderId,record.getId());
            List<OrderDetail> OrderDetaillist = orderDetailService.list(orderDetailLqw);
            ordersDto.setOrderDetails(OrderDetaillist);
            ordersDtos.add(ordersDto);
        }
        /*LambdaQueryWrapper<OrderDetail> orderDetailLqw = new LambdaQueryWrapper<>();
        orderDetailLqw.eq(OrderDetail::getNumber, userid);
        List<OrderDetail> detailList = orderDetailService.list(orderDetailLqw);

        OrdersDto ordersDto = new OrdersDto();
        ordersDto.setOrderDetails(detailList);
        ordersDtos.add(ordersDto);*/
        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }
}
